package com.shareach.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.mongodb.BasicDBObjectBuilder;
import com.shareach.common.BaseQueryOrder;
import com.shareach.common.BizCondition;
import com.shareach.dao.mongo.BizActivityShopDao;
import com.shareach.domain.BizActivityShopDomain;
import com.shareach.service.BizActivityShopService;
import com.shareach.service.exception.BaseService;
import com.shareach.util.Globals;

/**
 * create time：2013-2-1 下午1:11:55
 * 
 * @author retryu
 * @since JDK 1.6
 * @version 1.0 description：
 */

@Service
public class BizActivityShopServiceImpl extends BaseService<BizActivityShopDomain> implements BizActivityShopService {

	@Autowired
	private BizActivityShopDao bizActivityShopDao;

	@Override
	public synchronized Long save(BizActivityShopDomain domain) {
		domain.setCreatTime(new Date());
		Long id = bizActivityShopDao.insert(domain);
		return id;
	}

	@Override
	public synchronized Map<String, Object> saveOrUpdate(BizActivityShopDomain domain) {
		Map<String, Object> retMap = new HashMap<String, Object>();
		try {
			BizActivityShopDomain paraDomain = new BizActivityShopDomain();
			paraDomain.setActivityId(domain.getActivityId());
			paraDomain.setName(domain.getName());
			paraDomain.setAddr(domain.getAddr());

			BizActivityShopDomain retShop = bizActivityShopDao.findOne(parseDomaintoQuery(paraDomain));

			if (null != retShop) {
				paraDomain.setId(retShop.getId());
				update(domain, paraDomain);
				retMap.put(Globals.GLOBAL_SERVICE_RET_ID, retShop.getId());
				retMap.put(Globals.GLOBAL_SERVICE_RET_CODE, Globals.GLOBAL_SERVICE_CODE_EXIST);
				return retMap;
			} else {
				save(domain);
				retMap.put(Globals.GLOBAL_SERVICE_RET_ID, domain.getId());
				retMap.put(Globals.GLOBAL_SERVICE_RET_CODE, Globals.GLOBAL_SERVICE_CODE_NOTEXIST);
				return retMap;
			}
		} catch (Exception e) {
			e.printStackTrace();
			retMap.put(Globals.GLOBAL_SERVICE_RET_CODE, Globals.GLOBAL_SERVICE_CODE_ERROR);
			return retMap;
		}

	}

	@Override
	public void del(BizActivityShopDomain domain) {
		if (domain.getId() == null)
			return;
		Query query = new Query();
		query.addCriteria(Criteria.where("_id").is(domain.getId()));
		bizActivityShopDao.delete(query, BizActivityShopDomain.class);
	}

	@Override
	public synchronized void update(BizActivityShopDomain domain) {
		if (domain.getId() == null)
			return;
		Update update = parseDomaintoUpdate(domain);

		if (update.getUpdateObject().toMap().size() > 0) {
			bizActivityShopDao.update(Criteria.where("_id").is(domain.getId()), update);
		}
	}

	@Override
	public synchronized void update(BizActivityShopDomain domain, BizCondition contition) {
		// handle parse conditon
		Query query = parseConditionToQuery(contition);
		// handle update condition...
		Update update = parseDomaintoUpdate(domain);

		// update tels if necessary
		String[] tels = domain.getTels();
		if (tels != null && tels.length > 0) {
			update.addToSet("tels", BasicDBObjectBuilder.start("$each", tels).get());
		}
		// update discountWeeks if necessary
		Integer[] disCountWeeks = domain.getDiscountWeek();
		if (disCountWeeks != null && disCountWeeks.length > 0) {
			update.addToSet("discountWeek", BasicDBObjectBuilder.start("$each", disCountWeeks).get());
		}

		if (update.getUpdateObject().toMap().size() != 0) {
			bizActivityShopDao.update(query, update);
		}
	}

	@Override
	public BizActivityShopDomain getById(Long id) {
		if (id == null)
			return null;
		Query query = new Query();
		query.addCriteria(Criteria.where("_id").is(id));
		BizActivityShopDomain activityShop = bizActivityShopDao.findOne(query);
		return activityShop;
	}

	@Override
	public List<BizActivityShopDomain> getList(BizActivityShopDomain domain) {
		Query query = parseDomaintoQuery(domain);
		List<BizActivityShopDomain> bizActivityShopDomains = bizActivityShopDao.findList(query);
		return bizActivityShopDomains;
	}

	@Override
	public List<BizActivityShopDomain> getList(BizCondition condition) {
		Query query = parseConditionToQuery(condition);
		List<BizActivityShopDomain> domainList = bizActivityShopDao.findList(query);
		return domainList;
	}

	@Override
	public Page<BizActivityShopDomain> getPageResult(BizActivityShopDomain domain, BaseQueryOrder order) {
		Query query = parseDomaintoQuery(domain);
		Page<BizActivityShopDomain> domainPage = bizActivityShopDao.getPageResult(query, order);
		return domainPage;
	}

	@Override
	public void mactedShop(Long shopId, BizActivityShopDomain shop) {
		shop.setShopId(shopId);
		update(shop);
	}

	@Override
	public synchronized void update(BizActivityShopDomain domain, BizActivityShopDomain contition) {
		Update update = parseDomaintoUpdate(domain);
		Query query = parseDomaintoQuery(contition);
//		if (contition.getId() != null) {
//			query.addCriteria(Criteria.where("_id").is(contition.getId()));
//		}
		// update tels if necessary
		String[] tels = domain.getTels();
		if (tels != null && tels.length > 0) {
			update.addToSet("tels", BasicDBObjectBuilder.start("$each", tels).get());
		}
		// update discountWeeks if necessary
		Integer[] discountWeek = domain.getDiscountWeek();
		if (discountWeek != null && discountWeek.length > 0) {
			update.addToSet("discountWeek", BasicDBObjectBuilder.start("$each", discountWeek).get());
		}
		if (update.getUpdateObject().toMap().size() != 0) {
			bizActivityShopDao.update(query, update);
		}

	}

	@Override
	public List<BizActivityShopDomain> getListByIds(List<Long> ids, BaseQueryOrder order) {
		if (null != ids && ids.size() > 0) {
			Query qry = new Query();
			qry.addCriteria(Criteria.where("_id").in(ids));
			return bizActivityShopDao.getPageResult(qry, order).getContent();
		}
		return Lists.newArrayList();
	}

}
